package com.abc.recycling.service.impl;

import com.abc.recycling.config.BaiduMapConfig;
import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.MapService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.ArrayList;

@Slf4j
@Service
@RequiredArgsConstructor
public class MapServiceImpl implements MapService {
    private final BaiduMapConfig baiduMapConfig;
    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;
    private static final String BAIDU_MAP_API_URL = "https://api.map.baidu.com";

    @Override
    public List<RecyclingStation> getNearbyStations(Double latitude, Double longitude, Integer radius) {
        try {
            String url = String.format("%s/place/v2/search?query=回收站&location=%f,%f&radius=%d&output=json&ak=%s",
                    BAIDU_MAP_API_URL, latitude, longitude, radius, baiduMapConfig.getApiKey());
            
            String response = restTemplate.getForObject(url, String.class);
            JsonNode root = objectMapper.readTree(response);
            
            List<RecyclingStation> stations = new ArrayList<>();
            if (root.get("status").asInt() == 0) {
                JsonNode results = root.get("results");
                for (JsonNode result : results) {
                    RecyclingStation station = new RecyclingStation();
                    station.setName(result.get("name").asText());
                    station.setAddress(result.get("address").asText());
                    JsonNode location = result.get("location");
                    station.setLatitude(location.get("lat").asDouble());
                    station.setLongitude(location.get("lng").asDouble());
                    station.setStatus(1); // 默认营业中
                    stations.add(station);
                }
            }
            return stations;
        } catch (Exception e) {
            log.error("获取附近回收站失败", e);
            return new ArrayList<>();
        }
    }

    @Override
    public Double calculateDistance(Double startLat, Double startLng, Double endLat, Double endLng) {
        try {
            String url = String.format("%s/routematrix/v2/driving?origins=%f,%f&destinations=%f,%f&ak=%s",
                    BAIDU_MAP_API_URL, startLat, startLng, endLat, endLng, baiduMapConfig.getApiKey());
            
            String response = restTemplate.getForObject(url, String.class);
            JsonNode root = objectMapper.readTree(response);
            
            if (root.get("status").asInt() == 0) {
                return root.get("result").get(0).get("distance").get("value").asDouble();
            }
            return 0.0;
        } catch (Exception e) {
            log.error("计算距离失败", e);
            return 0.0;
        }
    }

    @Override
    public String getNavigationRoute(Double startLat, Double startLng, Double endLat, Double endLng) {
        try {
            String url = String.format("%s/direction/v2/driving?origin=%f,%f&destination=%f,%f&ak=%s",
                    BAIDU_MAP_API_URL, startLat, startLng, endLat, endLng, baiduMapConfig.getApiKey());
            
            String response = restTemplate.getForObject(url, String.class);
            JsonNode root = objectMapper.readTree(response);
            
            if (root.get("status").asInt() == 0) {
                return objectMapper.writeValueAsString(root.get("result"));
            }
            return "{}";
        } catch (Exception e) {
            log.error("获取导航路线失败", e);
            return "{}";
        }
    }
} 